// 接受请求数据

const port = 8080; // 端口

const express = require('express');
const application = express();

const staticRoot = 'public'; // 确定静态资源所在目录
const staticHandler = express.static(staticRoot);
application.use(staticHandler); // 处理静态资源

//使用url编码的有效负载解析传入请求
const urlencodedHandler = express.urlencoded({ extended: true });
application.use(urlencodedHandler);

application.get('/', (req, resp) => {
    console.log('GET /');
    console.log('request url : ', req.url);
    console.log('query string : ', req.query);
    console.log('route params : ', req.params);
    console.log('request body : ', req.body);

    resp.send(`<h1>${new Date().toLocaleString()}</h1>`);
});

application.post('/', (req, resp) => {
    console.log('POST /');
    console.log('request url : ', req.url);
    console.log('query string : ', req.query);
    console.log('route params : ', req.params);
    console.log('request body : ', req.body);

    resp.send(`<h1>${new Date().toLocaleString()}</h1>`);
});

// 如果需要在 路由路径 中指定参数名称，可以使用 ":名称" 形式来实现
application.get('/:name/:gender', (req, resp) => {
    console.log('GET /:name/:gender');
    console.log('request url : ', req.url);
    console.log('query string : ', req.query); // 对应 queryString
    console.log('route params : ', req.params); // 对应路由路径中的参数
    console.log('request body : ', req.body);

    resp.send(`<h1>${new Date().toLocaleString()}</h1>`);
});

application.listen(port, () => {
    console.log('启动服务器 (', __filename, ')');
});